package chapter_18

// getter setter

class Time {
  private[this] var h = 12
  private[this] var m = 12

  def hour: Int = h

  def hour_= (x: Int) {
    // 检查合法性
    require(0 <= x && x < 24)
    m = x
  }

  def minute = m

  def minute_= (x : Int) {
    // 检查合法性
    require(0 <= x && x < 60)
    m = x
  }

  override def toString = hour.toString + ":" + minute.toString
}


class Thermometer {
  // 摄氏度和华氏度的转换
  var celsius: Float = _

  def fahrenheit = celsius * 9 / 5 + 32
  def fahrenheit_= (f : Float) {  // 下划线和等号之间是没有空格的
    celsius = (f - 32) * 5 / 9
  }

  override def toString: String = fahrenheit + "F/" + celsius + "C"
}


object chapter_18 {

  def main(args: Array[String]): Unit = {
    val t = new Time
    println(t)
    t.minute = 5  // 如果大于了60或者小于0，这个就编译不过了
    println(t)

    // 温度转换
    val t2 = new Thermometer
    println(t2)
    t2.celsius = 100
    println(t2)
    t2.fahrenheit = -40
    println(t2)
  }
}
